package com.guofen.service.name.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.NameMapper;
import com.guofen.pojo.Name;
import com.guofen.pojo.User;
import com.guofen.service.name.NameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NameServiceImpl implements NameService {

    @Autowired
    private NameMapper mapper;

    @Override
    public Map<String, Object> getAllName(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Name> list = mapper.selectAll();
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByName(String name, int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Name> list = mapper.selectByName(name);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByGrade1(int grade1, int grade2, int pageSize, int currentPage) {
        if (grade1 > grade2) {
            int temp = grade1;
            grade1 = grade2;
            grade2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Name> list = mapper.selectByGrade1(grade1, grade2);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByGrade2(int grade1, int grade2, int pageSize, int currentPage) {
        if (grade1 > grade2) {
            int temp = grade1;
            grade1 = grade2;
            grade2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Name> list = mapper.selectByGrade2(grade1, grade2);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByGrade3(int grade1, int grade2, int pageSize, int currentPage) {
        if (grade1 > grade2) {
            int temp = grade1;
            grade1 = grade2;
            grade2 = temp;
        }
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<Name> list = mapper.selectByGrade3(grade1, grade2);
        PageInfo pageUser = new PageInfo(list);
        long sum = pageUser.getTotal();
        list = pageUser.getList();
        map.put("list",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Name getByName(String name) {
        return mapper.selectByPrimaryKey(name);
    }

    @Override
    public int delete(String name) {
        return mapper.deleteByPrimaryKey(name);
    }

    @Override
    public int update(Name name) {
        return mapper.updateByPrimaryKeySelective(name);
    }

    @Override
    public int insert(Name name) {
        return mapper.insertSelective(name);
    }
}
